<?php

class Formatter extends CFormatter {

	public function formatCurrency($value) {
		if (!$value) {
			return '-';
		}

		$data = $this->number($value);
		return "Rp. $data,-";
	}

	protected $_markdown = null;

	public function formatMarkdown($value) {
		if ($this->_markdown === null) {
			$this->_markdown = new CMarkdown;
		}
		return $this->_markdown->transform($value);
	}
	
	public function formatWordNumber($value) {
		return $this->toWord($value);
	}

	private function toWord($value) {
		$words = array(
			"",
			"satu",
			"dua",
			"tiga",
			"empat",
			"lima",
			"enam",
			"tujuh",
			"delapan",
			"sembilan",
			"sepuluh",
			"sebelas",
		);
		if ($value < 12)
			return " " . $words[$value];
		elseif ($value < 20)
			return $this->toWord($value - 10) . "belas";
		elseif ($value < 100)
			return $this->toWord($value / 10) . " puluh" . $this->toWord($value % 10);
		elseif ($value < 200)
			return " seratus" . $this->toWord($value - 100);
		elseif ($value < 1000)
			return $this->toWord($value / 100) . " ratus" . $this->toWord($value % 100);
		elseif ($value < 2000)
			return " seribu" . $this->toWord($value - 1000);
		elseif ($value < 1000000)
			return $this->toWord($value / 1000) . " ribu" . $this->toWord($value % 1000);
		elseif ($value < 1000000000)
			return $this->toWord($value / 1000000) . " juta" . $this->toWord($value % 1000000);
	}
	
	public function formatDate($value) {
		if ($value) return parent::formatDate($value);
		
		return '';
	}
	
	public function formatPercent($value) {
		$percent = (int) ($value * 100);
		return "{$percent}%";
	}

}
